package com.example.by.mdb.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.by.mdb.R;
import com.example.by.mdb.db.MyDatabaseHelper;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by BY on 2016/1/11.
 */
public class DianJian extends Activity implements View.OnClickListener {
    private Spinner indexSpinner, switchSpinner;
    private TextView itemTextView, unitTextView, upStandardTextview, lowStandardTextview, textView1, textView2, textView3;
    private EditText checkResultEditText, markEditText;
    private Button lastItemButton, nextItenButton, confirmButton, convertButton;
    private LinearLayout inspectionLayout;
    private ArrayAdapter<String> adapter, eqpAdapter;
    private ArrayList items, flags, units, upStandard, lowStandard, checkNo, eqpnoList, type, frequency, rst_ng, checkItem1, checkItem2, checkItem3;
    private SQLiteDatabase db;
    private ArrayList textAdapter1, textAdapter2, textAdapter3;
    private InputMethodManager imm;
    String table, eqpno = null;
    private boolean firstIn = true;
    private App app;
    private Date date;
    private String fileName;
    private LayoutInflater inflater;
    private int currentPosition = 0;
    private TextView hintTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.copy_editer_result);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras().getBundle("eqpno");
        eqpno = bundle.getString("name");
        table = bundle.getString("table");
        fileName = bundle.getString("fileName");
        eqpnoList = bundle.getStringArrayList("eqpno");
        int index = bundle.getInt("index");
        String s = eqpnoList.get(index) + "";
//        System.out.println(s);
        init();
        adjustSwitchData(index);
        for (int i = 0; i < eqpnoList.size(); i++) {
            if (eqpnoList.get(i).equals(s)) {
                index = i;
                break;
            }
        }
        initData(eqpno);
        eqpAdapter.setDropDownViewResource(R.layout.dropdown_item);
        switchSpinner.setAdapter(eqpAdapter);
        switchSpinner.setSelection(index);
        for (int i = 0; i < flags.size(); i++) {
            if (flags.get(i).equals("0")) {
                indexSpinner.setSelection(i);
                break;
            }
        }
        checkData();
        if (table.equals("pda_maintain")) {
            markEditText.setVisibility(View.GONE);
        }
//        spinner1.setVisibility(View.GONE);
    }

    private void adjustSwitchData(int position) {
        ArrayList index = new ArrayList();
        String s = (String) eqpnoList.get(position);
        s = s.substring(0, 3);
//        System.out.println(s);
        for (int i = 0; i < eqpnoList.size(); i++) {
            String name = (String) eqpnoList.get(i);
            if (s.equals(name.substring(0, 3))) {
                index.add(eqpnoList.get(i));
            }
        }
        eqpnoList.clear();
        for (int i = 0; i < index.size(); i++) {
            eqpnoList.add(index.get(i) + "");
            eqpAdapter.add(index.get(i) + "");
        }
    }

    private void clear() {
        frequency.clear();
        rst_ng.clear();
        items.clear();
        upStandard.clear();
        lowStandard.clear();
        type.clear();
        units.clear();
        flags.clear();
        checkNo.clear();
        adapter.clear();
        textAdapter1.clear();
        textAdapter2.clear();
        textAdapter3.clear();
        checkItem1.clear();
        checkItem2.clear();
        checkItem3.clear();
    }

    private void initData(String eqpno) {
        clear();
        Cursor cursor = db.query(table, null, "eqpno=? and file_name=?", new String[]{eqpno, fileName}, null, null, "checkno asc");
        if (cursor.moveToFirst()) {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                items.add(cursor.getString(cursor.getColumnIndex("item")));
                upStandard.add(cursor.getString(cursor.getColumnIndex("standard2")));
                lowStandard.add(cursor.getString(cursor.getColumnIndex("standard1")));
                units.add(cursor.getString(cursor.getColumnIndex("unit")));
                flags.add(cursor.getString(cursor.getColumnIndex("flag")));
                checkNo.add(cursor.getString(cursor.getColumnIndex("checkno")));
                type.add(cursor.getString(cursor.getColumnIndex("type")));
                frequency.add(cursor.getString(cursor.getColumnIndex("frequency")));
                rst_ng.add(cursor.getString(cursor.getColumnIndex("rst_ng")));
                String c1, c2, c3;
                c1 = cursor.getString(cursor.getColumnIndex("checkitem1"));
                c2 = cursor.getString(cursor.getColumnIndex("checkitem2"));
                c3 = cursor.getString(cursor.getColumnIndex("checkitem3"));
                adapter.add(c1);
//                System.out.println(c1 + "  " + c2 + "   " + c3);
                if (checkItem1.size() == 0) {
                    textAdapter1.add(c1);
                } else if (!checkItem1.get(checkItem1.size() - 1).equals(c1)) {
                    textAdapter1.add(c1);
                }
                if (checkItem2.size() == 0) {
                    textAdapter2.add(c2);
                } else if (!checkItem2.get(checkItem2.size() - 1).equals(c2)) {
                    textAdapter2.add(c2);
                }
                if (checkItem3.size() == 0) {
                    textAdapter3.add(c3);
                } else if (!checkItem3.get(checkItem3.size() - 1).equals(c3)) {
                    textAdapter3.add(c3);
                }
//                if (!checkItem1.contains(c1)) {
//                    textAdapter1.add(c1);
//                }
//                if (!checkItem2.contains(c2)) {
//                    textAdapter2.add(c2);
//                }
//                if (!checkItem3.contains(c3)) {
//                    textAdapter3.add(c3);
//                }
                checkItem1.add(c1);
                checkItem2.add(c2);
                checkItem3.add(c3);
//                System.out.println(checkItem3.size());
            }
//            adapter1.add(c1);
//            adapter2.add(c2);
//            adapter3.add(c3);
        }
        cursor.close();
        adapter.setDropDownViewResource(R.layout.dropdown_item);
        indexSpinner.setAdapter(adapter);
//        adapter1.setDropDownViewResource(R.layout.dropdown_item);
//        spinner1.setAdapter(adapter1);
//        adapter2.setDropDownViewResource(R.layout.dropdown_item);
//        spinner2.setAdapter(adapter2);
//        adapter3.setDropDownViewResource(R.layout.dropdown_item);
//        spinner3.setAdapter(adapter3);
        try {
            indexSpinner.setSelection(currentPosition);
            setposition(indexSpinner.getSelectedItemPosition());
        } catch (Exception e) {
            indexSpinner.setSelection(0);
            setText(0);
            currentPosition = 0;
        }

    }

    private void setText(int position) {
        unitTextView.setText("计量单位： " + units.get(position));
        upStandardTextview.setText("正常值上限： " + upStandard.get(position));
        lowStandardTextview.setText("正常值下限： " + lowStandard.get(position));
        itemTextView.setText("项目名称： " + items.get(position));
        int count = 0;
        int index = -1;
        for (int i = 0; i < flags.size(); i++) {
            if (flags.get(i).equals("0")) {
                count++;
                if (index < 0) {
                    index = i + 1;
                }
            }
        }
        int p = position + 1;
        hintTextView.setText("当前第" + p + "项       未点检首个项目位置第" + index + "项\n还剩" + count + "项未点检");
    }


//    private void setSelect(int position) {
//        for (int i = 0; i < adapter1.getCount(); i++) {
//            if (adapter1.getItem(i).equals(checkItem1.get(position))) {
//                spinner1.setSelection(i);
//            }
//        }
//        for (int i = 0; i < adapter2.getCount(); i++) {
//            if (adapter2.getItem(i).equals(checkItem2.get(position))) {
//                spinner2.setSelection(i);
//            }
//        }
//        for (int i = 0; i < adapter3.getCount(); i++) {
//            if (adapter3.getItem(i).equals(checkItem3.get(position))) {
//                spinner3.setSelection(i);
//                break;
//            }
//        }
//    }

    private void setposition(int position) {
        indexSpinner.setSelection(position);
//        for (int i = 0; i < adapter1.getCount(); i++) {
//            if (adapter1.getItem(i).equals(checkItem1.get(position))) {
//                spinner1.setSelection(i);
//            }
//        }
//        for (int i = 0; i < adapter2.getCount(); i++) {
//            if (adapter2.getItem(i).equals(checkItem2.get(position))) {
//                spinner2.setSelection(i);
//            }
//        }
//        for (int i = 0; i < adapter3.getCount(); i++) {
//            if (adapter3.getItem(i).equals(checkItem3.get(position))) {
//                spinner3.setSelection(i);
//            }
//        }
        textView1.setText(checkItem1.get(position) + "");
        textView2.setText(checkItem2.get(position) + "");
        textView3.setText(checkItem3.get(position) + "");
        setText(position);
        checkData();
    }

    private void init() {
        hintTextView = (TextView) findViewById(R.id.hint);
        inflater = LayoutInflater.from(getApplicationContext());
        app = (App) getApplication();
        MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(getApplicationContext(), "pda.db");
        db = myDatabaseHelper.getReadableDatabase();
        unitTextView = (TextView) findViewById(R.id.measureUnit);
        upStandardTextview = (TextView) findViewById(R.id.upperLimit);
        lowStandardTextview = (TextView) findViewById(R.id.lowerLimit);
        markEditText = (EditText) findViewById(R.id.markEditText);
        upStandardTextview.setVisibility(View.GONE);
        lowStandardTextview.setVisibility(View.GONE);
        checkResultEditText = (EditText) findViewById(R.id.checkResult);
        nextItenButton = (Button) findViewById(R.id.nextItem);
        lastItemButton = (Button) findViewById(R.id.lastItem);
        confirmButton = (Button) findViewById(R.id.confirmButton);
        convertButton = (Button) findViewById(R.id.convertButton);
        inspectionLayout = (LinearLayout) findViewById(R.id.inspectionLayout);
        inspectionLayout.setOnClickListener(this);
        nextItenButton.setOnClickListener(this);
        lastItemButton.setOnClickListener(this);
        confirmButton.setOnClickListener(this);
        convertButton.setOnClickListener(this);
        imm = (InputMethodManager) checkResultEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView3 = (TextView) findViewById(R.id.textView3);
        switchSpinner = (Spinner) findViewById(R.id.switchSpinner);
        indexSpinner = (Spinner) findViewById(R.id.fastFill);
        itemTextView = (TextView) findViewById(R.id.projectName);
        adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item);
        eqpAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item);
        textAdapter1 = new ArrayList();
        textAdapter2 = new ArrayList();
        textAdapter3 = new ArrayList();
        units = new ArrayList();
        flags = new ArrayList();
        upStandard = new ArrayList();
        lowStandard = new ArrayList();
        items = new ArrayList();
        checkNo = new ArrayList();
        type = new ArrayList();
        frequency = new ArrayList();
        rst_ng = new ArrayList();
        checkItem1 = new ArrayList();
        checkItem2 = new ArrayList();
        checkItem3 = new ArrayList();
        textView1.setOnClickListener(this);
        textView2.setOnClickListener(this);
        textView3.setOnClickListener(this);
        switchSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentPosition = indexSpinner.getSelectedItemPosition();
                initData((String) eqpnoList.get(position));
//                checkData();
//                if (!firstIn) {
//                    checkResultEditText.setText("");
//                }
//                if (firstIn == true) firstIn = !firstIn;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }

    private void insertOrUpdateMark() {
        if (table.equals("pda_check")) {
            String s = markEditText.getText().toString();
            Cursor cursor = db.query("last_edit", new String[]{"rst", "mark"}, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName}, null, null, null);
            if (cursor.moveToFirst()) {
                ContentValues values = new ContentValues();
                values.put("mark", s);
                db.update("last_edit", values, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName});

            }
            cursor.close();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nextItem:
//                insertData();
                insertOrUpdateMark();
                int position = indexSpinner.getSelectedItemPosition() + 1;
                if (position < adapter.getCount()) {
//                    setSelect(position);
                    setposition(position);
//                    checkData();
                } else {
                    Toast.makeText(getApplicationContext(), "已是最后一项", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.lastItem:
                insertOrUpdateMark();
//                insertData();
                int position1 = indexSpinner.getSelectedItemPosition() - 1;
//                if ()
                if (position1 > -1) {
//                    setSelect(position1);
                    setposition(position1);
//                    checkData();
//                    setInputType(position1);
                } else {
                    Toast.makeText(getApplicationContext(), "已是第一项", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.inspectionLayout:
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(checkResultEditText.getWindowToken(), 0);
                }
                break;
            case R.id.convertButton:
                if (convertButton.getText().toString().equals("+")) {
                    convertButton.setText("-");
                } else {
                    convertButton.setText("+");
                }
                break;
            case R.id.confirmButton:
                verifyData();
//                if (betweenRange) {
//                    int position2 = indexSpinner.getSelectedItemPosition() + 1;
//                    if (position2 < adapter.getCount()) {
//                        setposition(position2);
//                        checkData();
////                    setInputType(position2);
//                    } else {
//                        Toast.makeText(getApplicationContext(), "已是最后一项", Toast.LENGTH_SHORT).show();
//                    }
//                }
                break;
            case R.id.textView1:
                alertList(R.id.textView1, textAdapter1);
                break;
            case R.id.textView2:
                alertList(R.id.textView2, textAdapter2);
                break;
            case R.id.textView3:
                alertList(R.id.textView3, textAdapter3);
                break;
        }
    }

    private void alertList(final int Id, final ArrayList list) {
        final AlertDialog alert = new AlertDialog.Builder(DianJian.this).create();
        View view = inflater.inflate(R.layout.eqp_list, null);
        ListView listView = (ListView) view.findViewById(R.id.eqpListView);
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int position) {
                return list.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view1 = inflater.inflate(R.layout.eqp_list_item, null);
                TextView textView = (TextView) view1.findViewById(R.id.eqpTextView);
                textView.setText(list.get(position) + "");
                return view1;
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int z = 0;
                if (Id == R.id.textView1) {
                    for (int j = 0; j < position; j++) {
                        if ((textAdapter1.get(j) + "").equals(textAdapter1.get(position) + "")) {
                            z++;
                        }
                    }

                    if (position == 0) {
                        setposition(0);
                    } else {
                        int x = 0;
                        for (int i = 1; i < checkItem1.size(); i++) {
                            if ((!(list.get(position) + "").equals(checkItem1.get(i - 1) + "")) && ((checkItem1.get(i) + "").equals(list.get(position) + ""))) {
                                if (z == 0) {
                                    setposition(i);
                                    break;
                                } else {
                                    x++;
                                    if (x == z) {
                                        setposition(i);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                } else if (Id == R.id.textView2) {
                    for (int j = 0; j < position; j++) {
                        if ((textAdapter2.get(j) + "").equals(textAdapter2.get(position) + "")) {
                            z++;
                            System.out.println("z:" + z);
                        }
                    }
                    if (position == 0) {
                        setposition(0);
                    } else {
                        int x = 0;
                        for (int i = 1; i < checkItem2.size(); i++) {
                            if (z == 0) {
                                setposition(i);
                                break;
                            } else {
                                x++;
                                if (x == z) {
                                    setposition(i);
                                    break;
                                }
                            }
                        }
                    }
                } else if (Id == R.id.textView3) {
                    for (int j = 0; j < position; j++) {
                        if ((textAdapter3.get(j) + "").equals(textAdapter3.get(position) + "")) {
                            z++;
                        }
                    }
                    System.out.println("z:" + z);
                    if (position == 0) {
                        setposition(0);
                    } else {
                        int x = 0;
                        for (int i = 1; i < checkItem3.size(); i++) {
                            if ((!(list.get(position) + "").equals(checkItem3.get(i - 1) + "")) && ((checkItem3.get(i) + "").equals(list.get(position) + ""))) {
                                if (z == 0) {
                                    setposition(i);
                                    break;
                                } else {
                                    x++;
                                    if (x == z) {
                                        setposition(i);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                alert.dismiss();
            }
        });
        alert.setView(view);
        alert.show();
    }


    private void verifyData() {
        String editTextData = null;
        if (!checkResultEditText.getText().toString().equals("") || !markEditText.getText().toString().equals("")) {
            if (!checkResultEditText.getText().toString().equals("")) {

                if (convertButton.getText().toString().equals("+") || checkResultEditText.getText().toString().equals("0")) {
                    editTextData = checkResultEditText.getText().toString();
                } else {
                    editTextData = "-" + checkResultEditText.getText().toString();
                }
                if ((upStandard.get(indexSpinner.getSelectedItemPosition()).toString().equals("0") && lowStandard.get(indexSpinner.getSelectedItemPosition()).toString().equals("0"))
                        || (upStandard.get(indexSpinner.getSelectedItemPosition()).toString().equals("1") && lowStandard.get(indexSpinner.getSelectedItemPosition()).toString().equals("1"))) {
                    if (editTextData.equals("0") || editTextData.equals("1")) {
                        updateOrInsertData(editTextData);
                        int position = indexSpinner.getSelectedItemPosition() + 1;
                        if (position < adapter.getCount()) {
//                        setSelect(position);
                            setposition(position);
//                        checkData();
                        } else {
                            Toast.makeText(getApplicationContext(), "已是最后一项", Toast.LENGTH_SHORT).show();
                        }

                        convertButton.setText("+");
                    } else {
                        Toast.makeText(getApplicationContext(), "请输入0或1", Toast.LENGTH_SHORT).show();
                    }
                } else if (Float.parseFloat(editTextData) <= Float.parseFloat((String) upStandard.get(indexSpinner.getSelectedItemPosition())) && Float.parseFloat(editTextData) >= Float.parseFloat((String) lowStandard.get(indexSpinner.getSelectedItemPosition()))) {
                    updateOrInsertData(editTextData);
                    int position = indexSpinner.getSelectedItemPosition() + 1;
                    if (position < adapter.getCount()) {
//                        setSelect(position);
                        setposition(position);
//                        checkData();
                    } else {
                        Toast.makeText(getApplicationContext(), "已是最后一项", Toast.LENGTH_SHORT).show();
                    }

                    convertButton.setText("+");
                } else {
                    View view = inflater.inflate(R.layout.alert, null);
                    final AlertDialog alert = new AlertDialog.Builder(DianJian.this, AlertDialog.THEME_HOLO_LIGHT).create();
                    TextView message = (TextView) view.findViewById(R.id.alertText);
                    Button confirm = (Button) view.findViewById(R.id.confirm);
                    Button change = (Button) view.findViewById(R.id.change);
                    message.setText("请确认数值:" + editTextData);
                    final String finalEditTextData = editTextData;
                    confirm.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            updateOrInsertData(finalEditTextData);

                            int position = indexSpinner.getSelectedItemPosition() + 1;
                            if (position < adapter.getCount()) {
//                                setSelect(position);
                                setposition(position);
//                                checkData();
                            } else {
                                Toast.makeText(getApplicationContext(), "已是最后一项", Toast.LENGTH_SHORT).show();
                            }

                            convertButton.setText("+");
                            alert.dismiss();
                        }
                    });
                    change.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alert.dismiss();
                        }
                    });
                    alert.setView(view);
                    alert.show();
                }
            } else {
                updateOrInsertData("");

                int position = indexSpinner.getSelectedItemPosition() + 1;
                if (position < adapter.getCount()) {
//                    setSelect(position);
                    setposition(position);
//                    checkData();
                } else {
                    Toast.makeText(getApplicationContext(), "已是最后一项", Toast.LENGTH_SHORT).show();
                }

                convertButton.setText("+");
            }
        } else {
            convertButton.setText("+");

        }
    }

    private void updateOrInsertData(String data) {
        String editTextData = data;
        Cursor cursor = null;
        String tableName = null;
        if (table.startsWith("pda_check")) {
            tableName = "last_edit";
            cursor = db.query("last_edit", new String[]{"rst"}, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName}, null, null, null);
        } else {
            tableName = "result_maintain";
            cursor = db.query("result_maintain", new String[]{"rst"}, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName}, null, null, null);
        }
        if (cursor.moveToFirst()) {
            if (editTextData != cursor.getString(cursor.getColumnIndex("rst"))) {
                ContentValues values = new ContentValues();
                values.put("rst", editTextData);
                if (tableName.equals("last_edit")) {
                    values.put("mark", markEditText.getText().toString());
                    db.update(tableName, values, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName});
                } else {
                    db.update(tableName, values, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName});
                }
            }
        } else {
            insertData(editTextData);
        }

        if (cursor != null) {
            cursor.close();
        }
    }


    private void checkData() {
        Cursor cursor = null;
        if (table.equals("pda_check")) {
            cursor = db.query("last_edit", new String[]{"rst", "mark"}, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName}, null, null, null);
        } else {
            cursor = db.query("result_maintain", new String[]{"rst"}, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName}, null, null, null);
        }
        if (cursor.moveToFirst()) {
            String rst = cursor.getString(cursor.getColumnIndex("rst"));
            System.out.println(rst);
            checkResultEditText.setText(rst);
            if (table.equals("pda_check")) {
                markEditText.setText(cursor.getString(cursor.getColumnIndex("mark")));
                confirmButton.setClickable(false);
                confirmButton.setBackgroundResource(R.drawable.button_round_false);
            }

        } else {
            markEditText.setText("");
            checkResultEditText.setText("");
            confirmButton.setClickable(true);
            confirmButton.setBackgroundResource(R.drawable.button_round);
        }
        if (cursor != null) {
            cursor.close();
        }
    }

    private String formatTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        String currentTime = format.format(date);
        return currentTime;
    }

    private void insertData(String data) {
        date = new Date();
        String value = data;
        int position = indexSpinner.getSelectedItemPosition();
        flags.set(position, "1");
        ContentValues values = new ContentValues();
        values.put("flag", "1");
        db.update(table, values, "eqpno=? and checkno=? and file_name=?", new String[]{(String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(indexSpinner.getSelectedItemPosition()), fileName});
        if (table.equals("pda_check")) {
            db.execSQL(
                    "insert into last_edit(rst,eqpno,checkno,frequency,lastedit,file_name,mark) values(?,?,?,?,?,?,?)",
                    new String[]{value, (String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(position), (String) frequency.get(position), formatTime(date), fileName, markEditText.getText().toString()});
        } else {
            db.execSQL(
                    "insert into result_maintain(rst,eqpno,checkno,empno,ng,rst_ng,aim,select_index,frequency,check_time,file_name) values(?,?,?,?,?,?,?,?,?,?,?)",
                    new String[]{value, (String) eqpnoList.get(switchSpinner.getSelectedItemPosition()), (String) checkNo.get(position), app.getEmpno(), "0", "2", "0", "0", (String) frequency.get(position), formatTime(date), fileName});
            ;
        }


    }
}
